# -*- encoding=utf8 -*-

__author__ = "code哥"
import asyncio
import json
from sanic.blueprints import Blueprint
from sanic_openapi import doc
from conn_pool import get_redis, REDIS_SCRIPT_DEVICE_EXECUTION_HEALTH

rs = get_redis()

blueprint = Blueprint(__name__, '/api')


@doc.summary("websocket 推送当前执行健康信息")
async def feed(request, ws):
    while True:
        redis_health_list = rs.get(REDIS_SCRIPT_DEVICE_EXECUTION_HEALTH)
        if redis_health_list is not None and len(str(redis_health_list)) > 0:
            health_list = json.loads(redis_health_list, encoding='utf-8')
            resp = json.dumps(health_list, ensure_ascii=False)
            print('send health resp: ', resp)
            await ws.send(resp)
        await asyncio.sleep(5)


blueprint.add_websocket_route(feed, '/health_exec_ws')
